package com.cheyunkeji.er.activity.evaluate;

import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;

import com.cheyun.netsalev3.R;
import com.cheyun.netsalev3.MyApplication;
import com.cheyunkeji.er.base.BaseActivity;
import com.cheyunkeji.er.bean.evaluate.EvaluateDetailResult2;
import com.cheyunkeji.er.http.ApiClient;
import com.cheyunkeji.er.http.HttpConstants;
import com.cheyunkeji.er.http.RespCallback;
import com.cheyunkeji.er.view.SToast;
import com.cheyunkeji.er.view.TopBar;
import com.cheyunkeji.er.view.evaluate.BCSMCheckItemView;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import okhttp3.Request;

public class FunctionalityPartsCheckBCSM extends BaseActivity {

  private static final String TAG = FunctionalityPartsCheckBCSM.class.getSimpleName();

  @BindView(R.id.v_topbar)
  TopBar vTopbar;

  @BindView(R.id.bcsm_177)
  BCSMCheckItemView bcsm177;

  @BindView(R.id.bcsm_178)
  BCSMCheckItemView bcsm178;

  @BindView(R.id.bcsm_179)
  BCSMCheckItemView bcsm179;

  @BindView(R.id.bcsm_180)
  BCSMCheckItemView bcsm180;

  @BindView(R.id.bcsm_181)
  BCSMCheckItemView bcsm181;

  @BindView(R.id.bcsm_182)
  BCSMCheckItemView bcsm182;

  @BindView(R.id.bcsm_183)
  BCSMCheckItemView bcsm183;

  @BindView(R.id.bcsm_184)
  BCSMCheckItemView bcsm184;

  @BindView(R.id.bcsm_185)
  BCSMCheckItemView bcsm185;

  @BindView(R.id.bcsm_186)
  BCSMCheckItemView bcsm186;

  @BindView(R.id.activity_functionality_parts_check_bcsm)
  LinearLayout activityFunctionalityPartsCheckBcsm;

  private Map<Integer, BCSMCheckItemView> itemViewList = null;

  private List<EvaluateDetailResult2.DetectionBean.PartsMarkBean> partsMarkList = null;

  @Override
  protected void init() {

    setContentView(R.layout.er_activity_functionality_parts_check_bcsm);
    ButterKnife.bind(this);

    itemViewList = new HashMap<>();
    itemViewList.put(177, bcsm177);
    itemViewList.put(178, bcsm178);
    itemViewList.put(179, bcsm179);
    itemViewList.put(180, bcsm180);
    itemViewList.put(181, bcsm181);
    itemViewList.put(182, bcsm182);
    itemViewList.put(183, bcsm183);
    itemViewList.put(184, bcsm184);
    itemViewList.put(185, bcsm185);
    itemViewList.put(186, bcsm186);
  }

  @Override
  protected void initView() {

    vTopbar.setLeftBack();
    vTopbar.setRight("保存", -1, this);
    vTopbar.setTitle("功能性零部件补充说明");

    if (null != MyApplication.getInstance().getEvaluateResult().getDetection()
        && null != MyApplication.getInstance().getEvaluateResult().getDetection().getParts_mark()) {

      partsMarkList =
          MyApplication.getInstance().getEvaluateResult().getDetection().getParts_mark();

      if (partsMarkList.size() > 0) {

        displayInfo();
      }
    }
  }

  private void displayInfo() {

    for (EvaluateDetailResult2.DetectionBean.PartsMarkBean bean : partsMarkList) {

      itemViewList.get(bean.getId()).setCheckedIndex(bean.getStatus() - 1);
      itemViewList.get(bean.getId()).setTag(bean);
    }
  }

  @Override
  protected void initData() {}

  @Override
  public void onClick(View v) {
    super.onClick(v);

    if (partsMarkList == null) {
      partsMarkList = new ArrayList<>();
    } else {
      partsMarkList.clear();
    }

    if (v.getId() == R.id.vRight) {

      for (int index : itemViewList.keySet()) {
        if (3 != itemViewList.get(index).getCurrentCheckedIndex()) {

          partsMarkList.add(
              new EvaluateDetailResult2.DetectionBean.PartsMarkBean(
                  index, itemViewList.get(index).getCurrentCheckedIndex() + 1));
        }
      }

      String myData =
          new StringBuilder("{\"parts_mark\":")
              .append(new Gson().toJson(partsMarkList))
              .append("}")
              .toString();

      Log.e(TAG, "onClick: " + new Gson().toJson(partsMarkList));

      HashMap<String, String> params = new HashMap<>();
      //            params.put("authkey",
      // UserInfoManager.getInstance(MyApplication.getInstance()).getAuthKey());
      //            params.put(ChannelInfoUtil.getCurrentChannelInfo().getCkey1().getYkey(),
      // ChannelInfoUtil.getCurrentChannelInfo().getCkey1().getY7e_e73a691f02a46500201c3998e6a2989d());
      params.put("aid", MyApplication.getInstance().getEvaluateResult().getAid());

      params.put("detection", myData);

      ApiClient.postForm(
          HttpConstants.INSERT_ARCHIVE_INFO,
          params,
          new RespCallback<EvaluateDetailResult2>() {

            @Override
            protected void onRequestFailed(String msg) {
              SToast.show(msg);
            }

            @Override
            protected void onSuccess(EvaluateDetailResult2 data) {
              if (null != data) {
                MyApplication.getInstance().setEvaluateResult(data);
                SToast.show("保存成功");
                MyApplication.getInstance().removeActivity(FunctionalityPartsCheckBCSM.class);
                finish();
              }
            }

            @Override
            public void onBefore(Request request, int id) {
              showDialog();
            }

            @Override
            public void onAfter(int id) {
              dismissDialog();
            }
          });
    }
  }
}
